package cn.yyh.servicePet.service.impl;

import cn.yyh.commons.service.impl.MyBaseServiceImpl;
import cn.yyh.commons.utils.CodeGenerateUtils;
import cn.yyh.commons.utils.PageInfo;
import cn.yyh.servicePet.client.EmpClient;
import cn.yyh.servicePet.client.PetAcquisitionOrderClient;
import cn.yyh.servicePet.client.ShopClient;
import cn.yyh.servicePet.client.UserClient;
import cn.yyh.servicePet.domain.*;
import cn.yyh.servicePet.mapper.PetMapper;
import cn.yyh.servicePet.mapper.SearchMasterMsgMapper;
import cn.yyh.servicePet.query.SearchMasterMsgQuery;
import cn.yyh.servicePet.service.IPetService;
import cn.yyh.servicePet.service.ISearchMasterMsgService;
import cn.yyh.servicePet.utils.ConvertUtils;
import cn.yyh.servicePet.utils.DistanceUtil;
import cn.yyh.servicePet.utils.Point;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 杨永航
 * @version 1.0
 * @Title: SearchMasterMsgServiceImpl
 * @Package: cn.yyh.pet.service.impl
 * @date 2021/7/5
 */
@Service
public class SearchMasterMsgServiceImpl extends MyBaseServiceImpl<SearchMasterMsgMapper, SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private ShopClient shopClient;
    @Autowired
    private IPetService petService;
    @Autowired
    private EmpClient empClient;

    @Autowired
    private UserClient userClient;
    @Autowired
    PetAcquisitionOrderClient orderClient;


    @Override
    public void publish(SearchMasterMsg searchMasterMsg, Long id,String token) {
        String infoId = userClient.getByLoginInfoId(id, token);
        User user = ConvertUtils.toUser(ConvertUtils.user(infoId));
        searchMasterMsg.setUser_id(user.getId());
        searchMasterMsg.setUser(user);
        //2.获取最近的店铺
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        String all = shopClient.listAll(token);
        List<Shop> shops = ConvertUtils.toListShop(ConvertUtils.shopListAll(all));
        Shop nearestShop = DistanceUtil.getNearestShop(point, shops);
        searchMasterMsg.setShop(nearestShop);
        searchMasterMsg.setShop_id(nearestShop.getId());
        //3.保存寻主消息
        baseMapper.save(searchMasterMsg);

    }

    @Override
    public PageInfo<SearchMasterMsg> queryPage(SearchMasterMsgQuery query, Long id,String token) {

        String infoId = empClient.getByLoginInfoId(id, token);
        Employee emp = ConvertUtils.toEmp(ConvertUtils.emp(infoId));
        if (emp.getShopId() != null) {
            query.setShopId(emp.getShopId());
        }

        return super.pageQuery(query);
    }

    @Override
    public void handlePets(Pet pet, Long id,String token) {
        baseMapper.updateStateById(pet.getSearch_master_msg_id());
        SearchMasterMsg masterMsg = baseMapper.getById(pet.getSearch_master_msg_id());
        pet.setUser_id(masterMsg.getUser_id());
        petService.save(pet);
        //3.生成订单
        String infoId = empClient.getByLoginInfoId(id, token);
        Employee employee = ConvertUtils.toEmp(ConvertUtils.emp(infoId));
        PetAcquisitionOrder acquisitionOrder = pet2order(pet, masterMsg, employee.getId());
        orderClient.save(acquisitionOrder,token);
        //@TODO 4.支付单并支付
        if(acquisitionOrder.getPrice().longValue() > 0){
            System.out.println("下单成功！支付金额是："+acquisitionOrder.getPrice());
        }


    }

    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg adopt, Long empId) {
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
        order.setState(0);//待支付
        order.setPrice(pet.getCostprice());
        order.setAddress(adopt.getAddress());
        String orderSn = CodeGenerateUtils.generateOrderSn(adopt.getUser_id());
        order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
        order.setUser_id(adopt.getUser_id());
        order.setPaytype(0);
        order.setShop_id(pet.getShop_id());
        order.setEmp_id(empId);
        return order;
    }

}
